﻿#pragma once
#include "Task.h"
#if (COMM_FEATURE_LIBEVENT==1)

#define HTTPSERVER_DEFAULT_COTENTTYPE	"text/plain;charset=utf-8"

namespace COMM
{
#ifdef COMM_MSVC
	template class COMM_API std::list<void*>;
#endif

	class COMM_API HttpRequestHandler
	{
	public:
		virtual ~HttpRequestHandler() {}
		virtual void Release() { delete this; }
		virtual bool OnRequest(const char* uri, const char *cmdType, const StringList& requestHeaders, const char* requestContent,
			StringList& responseHeaders, Buffer& responseContent) = 0;
	};

	class COMM_API HttpServer
	{
	public:
		HttpRequestHandler* m_handler;
		int m_port;
		String m_ip;
		int m_timeout_seconds;
		int m_thread_count_per_core;
		int m_core_count;
		String m_content_type;
		String m_crtfile;
		String m_keyfile;
		COMM_SOCKET m_sock;

		HttpServer(HttpRequestHandler* handler,
			int listenPort, const char* listenIP = "0.0.0.0",
			int timeoutSeconds = 20, 
			int threadCountPerCore = 4, int coreCount = 1,
			const char* contentType = HTTPSERVER_DEFAULT_COTENTTYPE,
			const char* crtfile = NULL, const char* keyfile = NULL);
		~HttpServer();

		bool Start();
		void Stop();

	private:
		ThreadLock m_lock;
		std::list<void*> m_instance_list;
		bool BindSocket();
		void CloseSocket();

	};
}

#endif